package com.hbzhit.modules.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hbzhit.common.extension.BaseController;
import com.hbzhit.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.hbzhit.modules.sys.entity.ProUserEntity;
import com.hbzhit.modules.sys.service.ProUserService;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 项目成员表
 *
 * @author 1
 * @date 2022-04-28 10:31:49
 */
@Api(tags = {"项目成员表"})
@AllArgsConstructor
@RestController
@RequestMapping("sys/proUser")
public class ProUserController extends BaseController<ProUserService, ProUserEntity> {

    @Resource
    private ProUserService proUserService;

    @GetMapping("/getProUserList")
    @ApiOperation("获取项目人员列表")
    public R<?> getProUserList(@RequestParam Long proId) {
        return R.ok().data(proUserService.queryListByProId(proId));
    }


    @Override
    public R<ProUserEntity> save(@RequestBody ProUserEntity proUserEntity) {
        ProUserEntity oldProUser = proUserService.getOne(Wrappers.<ProUserEntity>lambdaQuery()
                .eq(ProUserEntity::getProId, proUserEntity.getProId()).eq(ProUserEntity::getUserId, proUserEntity.getUserId()).eq(ProUserEntity::getIsDel, 1));
        if (ObjectUtil.isNotEmpty(oldProUser)) {
            proUserEntity.setId(oldProUser.getId());
            proUserEntity.setIsDel(0);
            proUserService.updateById(proUserEntity);
            return R.ok(proUserEntity);
        } else {
            return super.save(proUserEntity);
        }
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @PostMapping("/delete")
    @Override
    public R<Object> delete(@RequestBody Long[] ids){
        baseService.update(Wrappers.<ProUserEntity>update().set("is_del", 1).in("id", Arrays.asList(ids)));
        return R.ok();
    }
}
